package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartControllert {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        //当前用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId).orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /*
    * 添加菜品到购物车
    * */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加到购物车的菜品：{}",shoppingCart.toString());
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询当前菜品或者套餐是否在购物车中，
        Long dishId = shoppingCart.getDishId();
        // String dishFlavor1 = shoppingCart.getDishFlavor();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (dishId!=null){
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //根据条件查询购物车中的菜品
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne!=null){
            //存在，只要number+1
            //修改：存在的是菜品的话，需要对照口味是否一致，口味一致则（dishFlavor）。
            /*if (dishId!=null){
                String dishFlavor2 = cartServiceOne.getDishFlavor();
                if (!dishFlavor2.equals(dishFlavor1)){
                    //口味不一致，添加到购物车数据库中
                    shoppingCart.setCreateTime(LocalDateTime.now());
                    shoppingCartService.save(shoppingCart);
                }else {
                    cartServiceOne.setNumber(cartServiceOne.getNumber()+1);
                    shoppingCart.setCreateTime(LocalDateTime.now());
                    shoppingCartService.updateById(cartServiceOne);
                }
            }*/
            cartServiceOne.setNumber(cartServiceOne.getNumber()+1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //不存在
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /*
    * 清空购物车
    * */
    @DeleteMapping("/clean")
    public R<String> clear(){
        //删除当前用户的所有订单
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null, ShoppingCart::getUserId,userId);
        boolean remove = shoppingCartService.remove(queryWrapper);
        if (remove){
            return R.success("清空购物车成功");
        }else {
            return R.error("清空购物车失败");
        }
    }

    /*
    * 减少菜品数量
    * */
    @PostMapping("sub")
    public R<ShoppingCart> subtractCart(@RequestBody ShoppingCart shoppingCart){
        log.info("减少商品的信息：{}",shoppingCart.toString());
        //当前登录用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //减少的是菜品
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (shoppingCart.getDishId()!=null){
            //dishid对应的numbe减1，如果number=1，再减少就删除数据库数据
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
            if (one.getNumber()>1){
                one.setNumber(one.getNumber()-1);
                shoppingCartService.updateById(one);
            }else {
                shoppingCartService.removeById(one.getId());
            }
            ShoppingCart one1 = shoppingCartService.getOne(queryWrapper);
            if (one1==null){
                shoppingCart.setNumber(0);
            }else{
                shoppingCart=one1;
            }

        }
        //减少的是套餐
        if (shoppingCart.getSetmealId()!=null){
            //SetmealId对应的numbe减1，如果number=1，再减少就删除数据库数据
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
            if (one.getNumber()>1){
                one.setNumber(one.getNumber()-1);
                shoppingCartService.updateById(one);
            }else {
                shoppingCartService.removeById(one.getId());
            }
            ShoppingCart one1 = shoppingCartService.getOne(queryWrapper);
            if (one1==null){
                shoppingCart.setNumber(0);
            }else{
                shoppingCart=one1;
            }
        }
        return R.success(shoppingCart);
    }
}
